package com.hdyg.sixg.activity.trans;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.CertifAdapter;
import com.hdyg.sixg.adapter.ImageAdapter;
import com.hdyg.sixg.entry.BuySellDetailBean;
import com.hdyg.sixg.entry.ImageBean;
import com.hdyg.sixg.mvp.Contrant.CBuyDetail;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PBuyDetail;
import com.hdyg.sixg.util.FileSizeUtil;
import com.hdyg.sixg.util.LogUtils;
import com.hdyg.sixg.util.TakePhotoUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 我的购买详情
 * @time 2019/4/9 16:00
 */
public class BuyDetailActivity extends BaseActivity implements CBuyDetail.IVBuyDetail {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_buy_1)
    SuperTextView svBuy1;
    @BindView(R.id.sv_buy_2)
    SuperTextView svBuy2;
    @BindView(R.id.sv_buy_3)
    SuperTextView svBuy3;
    @BindView(R.id.sv_buy_4)
    SuperTextView svBuy4;
    @BindView(R.id.sv_buy_5)
    SuperTextView svBuy5;
    @BindView(R.id.sv_buy_6)
    SuperTextView svBuy6;
    @BindView(R.id.sv_buy_7)
    SuperTextView svBuy7;
    @BindView(R.id.sv_buy_8)
    SuperTextView svBuy8;
    @BindView(R.id.sv_buy_9)
    SuperTextView svBuy9;
    @BindView(R.id.sv_buy_10)
    SuperTextView svBuy10;
    @BindView(R.id.sv_buy_11)
    SuperTextView svBuy11;
    @BindView(R.id.sv_buy_12)
    SuperTextView svBuy12;
    @BindView(R.id.sv_buy_13)
    SuperTextView svBuy13;
    @BindView(R.id.rv_image)
    RecyclerView rvImage;
    @BindView(R.id.iv_add)
    ImageView ivAdd;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CertifAdapter imageAdapter;
    private List<String> imageBeanList;
    private CBuyDetail.IPBuyDetail mPresenter;
    private String id, pic;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_buy_detail;
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra(SpMsg.INTENT_STR);
        onClickListener();
        imageBeanList = new ArrayList<>();
        tvTopTitle.setText(R.string.detail_buy_title);
        rvImage.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        initAdapter();
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        //添加图片
        ivAdd.setOnClickListener(v -> {
            int size = imageAdapter.getData().size();
            if (size >= CertifAdapter.IMAGE_MAX_NUMBER) {
                return;
            }
            TakePhotoUtil.getInstance().sustomPhotoMult(mContext, CertifAdapter.IMAGE_MAX_NUMBER - size, list -> {
                for (int i = 0; i < list.size(); i++) {
                    double imgSize = FileSizeUtil.getFileOrFilesSize(list.get(i).getOriginalPath(), FileSizeUtil.SIZETYPE_MB);//图片大小
                    LogUtils.e("图片大小==>" + imgSize);
                    //显示图片小于2M才能上传
                    if (imgSize <= BaseUrl.IMG_SIZE_LIMIT) {
                        File file = new File(list.get(i).getOriginalPath());
                        mPresenter.pGetUploadImg(BaseUrl.UPLOAD_URL, file, "picture", GetParamUtil.getUploadImageParam());
                    } else {
                        String str = String.format(getResources().getString(R.string.toast_image_size), String.valueOf(BaseUrl.IMG_SIZE_LIMIT));
                        toastNotifyShort(str);
                    }
                }
            });
        });
        //确认提交
        btSure.setOnClickListener(v -> {
            List<String> list = imageAdapter.getData();
            if (list.size() != 0) {
                StringBuffer buffer1 = new StringBuffer();
                for (int i = 0; i < list.size(); i++) {
                    buffer1.append(list.get(i)).append(",");
                }
                pic = buffer1.substring(0, buffer1.length() - 1);
            }
            if (TextUtils.isEmpty(pic)) {
                toastNotifyShort(R.string.toast_image);
                return;
            }
            mPresenter.pGetApplyBuyDetail(BaseUrl.TRAD_PIC_URL, GetParamUtil.getTradPicParam(id, pic));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.SELL_DETAIL_URL, GetParamUtil.getSellDetailParam(id));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PBuyDetail(this);
    }

    private void initAdapter() {
        imageAdapter = new CertifAdapter(R.layout.item_take_photo, imageBeanList);
        rvImage.setAdapter(imageAdapter);
        //删除图片
        imageAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            int id = view.getId();
            if (id == R.id.ivDelete) {
                imageAdapter.remove(position);
            }
        });
    }

    private void setUI(String status) {
        switch (status) {
            case "1":
                ivAdd.setVisibility(View.VISIBLE);//显示上传按钮
                btSure.setVisibility(View.VISIBLE);//显示确认订单
                break;
            case "2":
                ivAdd.setVisibility(View.GONE);//隐藏上传按钮
                btSure.setVisibility(View.VISIBLE);//显示确认订单
                break;
            case "3":
                ivAdd.setVisibility(View.GONE);//隐藏上传按钮
                btSure.setVisibility(View.GONE);//隐藏确认订单
                break;
        }
    }

    @Override
    public void vGetInitSuccess(BuySellDetailBean dataBean) {
        if ("1".equals(dataBean.getType())) {
            setUI(dataBean.getStatus());
        }
        svBuy1.setRightString(dataBean.getText_status());
        svBuy2.setRightString(dataBean.getUsername());
        svBuy3.setRightString(dataBean.getPrice());
        svBuy4.setRightString(dataBean.getAmount());
        svBuy5.setRightString(dataBean.getTotal_price());
        svBuy7.setRightString(dataBean.getCreated_at());
        svBuy8.setRightString(dataBean.getPay_at());
        svBuy9.setRightString(dataBean.getFinish_time());
        svBuy11.setRightString(dataBean.getTruename());
        svBuy12.setRightString(dataBean.getAlipay());
        svBuy13.setRightString(dataBean.getPhone());
        imageAdapter.addData(dataBean.getTradepic());
    }

    @Override
    public void vGetUploadImgSuccess(ImageBean dataBean) {
        imageAdapter.addData(dataBean.getSrc());
    }

    @Override
    public void vGetApplyBuyDetailSuccess(String dataBean) {
        toastNotifyShort(dataBean);
    }
}
